var camera, scene, renderer, controls, effect;
var auto = false;
//var auto = true;
var room = {
	//墙体形状
	wallZ:(function(){
		var pts = [];
		pts.push( new THREE.Vector2 (0,0) );
		pts.push( new THREE.Vector2 (0,-10) );
		pts.push( new THREE.Vector2 (-100,-10) );
		pts.push( new THREE.Vector2 (-100,0) );
		return new THREE.Shape( pts );
	})(),
	//墙体形状
	wallX:(function(){
		var pts = [];
		pts.push( new THREE.Vector2 (0,0) );
		pts.push( new THREE.Vector2 (0,100) );
		pts.push( new THREE.Vector2 (10,100) );
		pts.push( new THREE.Vector2 (10,0) );
		return new THREE.Shape( pts );
	})(),
	addTool : function (data){
		var height = window.innerHeight;
		var toolHtml = "";
		var toolHeight = 0;
		for(var i=0;i<data.length;i++){
			toolHtml += '<div id="'+data[i].id+'" style="width: 40px;height: 40px;line-height: 40px;text-align: center;cursor: pointer;margin: 10px;">'+data[i].name+'</div>';
			toolHeight += 50;
		}
		var top = (height-toolHeight)/2;
		var html = "<div id='_tool' style='background-color:#ffeeff;opacity: 0.7;position: absolute;top:"+top+"px;z-index: 6670;width: 60px;'>";
		$("body").append(html+toolHtml+"</div>");
		$("#_tool div").hover(function(){
			$(this).css({"background-color":"#ffeedd"});
		},function(){
			$(this).css({"background-color":"#ffffff"});
		});
	},
	/**
	 * 相机
	 * @param dom
	 */
	setCamera:function(dom){
		camera = new THREE.PerspectiveCamera( 40, dom.width()/dom.height(), 1, 20000 );
		camera.position.set( 0, 400, 400 * 3.5 );
//		camera.position.set( 8, 10, 8 );
	},
	/**
	 * 设置场景
	 */
	setScene:function(){
		var urls = ["5.jpg",	"3.jpg",	"6.jpg",	"4.jpg",	"2.jpg","1.jpg"];
		var reflectionCube = new THREE.CubeTextureLoader()
			.setPath( 'img/' )
			.load( urls );
		reflectionCube.format = THREE.RGBFormat;
		scene = new THREE.Scene();
		scene.background = reflectionCube;
//		scene.position.setX(-500);
//		scene.position.setZ(-1000);
//		scene.position.setY(-1000);
		scene.add( new THREE.AmbientLight( "#eeeeee") );//环境光源
	},
	/**
	 * 添加文字
	 * @param opt{} text,color,position
	 */
	addText:function(opt){
		var loader = new THREE.FontLoader(); 
//		 loader.load("fonts/gentilis_regular.typeface.json", function (res) {
		 loader.load("fonts/FZLanTingHeiS-UL-GB_Regular.json", function (res) {
			 var font = new THREE.TextBufferGeometry(opt.text, {
				  font: res,
				  size: 20,
				  height: 1
			  }); 
			  font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象，如果不运行无法获得。 
			  font.computeVertexNormals(); 
			  var material = new THREE.MeshLambertMaterial({side:THREE.DoubleSide,color: opt.color}); 
			  fontModel = new THREE.Mesh(font,material); 
			  fontModel.position.set.apply(fontModel.position,opt.position);
			  fontModel.rotation.x = -1.55;
			  scene.add(fontModel); 
		 }); 
	},
	/**
	 * 添加线 obj:color,positions
	 */
	addLine:function(obj){
		var geometry_line = new THREE.Geometry();   //声明一个几何体
        var material_line = new THREE.LineBasicMaterial({ color: obj.color,linewidth: 5, });   //定义材质外观
        for(var i=0;i<obj.positions.length;i++){
        	var p = obj.positions[i];
        	var point = new THREE.Vector3(p[0],p[1],p[2]);
        	geometry_line.vertices.push(point); //vertices属性里可以存放点对象
        }
        var line = new THREE.Line(geometry_line, material_line);    //定义一条线
        scene.add(line);   //加入场景里
	},
	/**
	 * 添加立方体 obj:imgs,lwg,position
	 */
	addCube:function(obj){
		var materials = [];
        for (var i = 0; i < obj.imgs.length; ++i) {
            materials.push(new THREE.MeshBasicMaterial({
                map: THREE.ImageUtils.loadTexture(obj.imgs[i],
                    {}, function() {
                    	renderer.render( scene, camera );
                }),
            overdraw: true
            }));
        }
        var cube = new THREE.Mesh(new THREE.CubeGeometry(obj.lwg[0],obj.lwg[1],obj.lwg[2]),new THREE.MeshFaceMaterial(materials));
        cube.aa = {name:"立方体"};
        cube.position.set.apply(cube.position,obj.position);
        scene.add(cube);
	},
	/**
	 * 画墙体obj :color,points,opa
	 */
	addWall:function(obj){
		var pointsa = [new THREE.Vector3(obj.points[0][0],obj.points[0][1],obj.points[0][2]),new THREE.Vector3(obj.points[1][0],obj.points[1][1],obj.points[1][2])];
		var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
		//两个点x只相等，z轴方向墙体
		var type = obj.points[0][0] == obj.points[1][0]?room.wallZ:room.wallX;
	    var geometrya = new THREE.ExtrudeGeometry( type, {
	        steps	: 2,
	        extrudePath: randomSplinea
    	});
	  //透明,模拟玻璃
	    var materialssa = obj.opa?new THREE.MeshBasicMaterial({color:obj.color,transparent:true,blending:THREE.AdditiveBlending}):new THREE.MeshLambertMaterial({color:obj.color,wireframe: false });
		var linea = new THREE.Mesh(geometrya,materialssa);
		scene.add(linea);
	},
	/**
	 * 画地板obj : start,end,color
	 */
	addFloor:function(obj){
		var pts = [];
		pts.push( new THREE.Vector2 (obj.start[0],obj.start[1]) );
		pts.push( new THREE.Vector2 (obj.start[0],obj.end[1]) );
		pts.push( new THREE.Vector2 (obj.end[0],obj.end[1]));
		pts.push( new THREE.Vector2 (obj.end[1],obj.start[0]) );
		var shape = new THREE.Shape( pts );
		
		var pointsa = [new THREE.Vector3(0,-10,0),new THREE.Vector3(0,0,0)];
		var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
	    var geometrya = new THREE.ExtrudeGeometry( shape, {
	        steps	: 2,
	        extrudePath: randomSplinea
    	});
	    var materialssa = new THREE.MeshLambertMaterial({color:obj.color,wireframe: false });
		var linea = new THREE.Mesh(geometrya,materialssa);
		scene.add(linea);
	},
	/**
	 * 电源进线data color
	 */
	addPowerLines:function(data,color){
		for(var i=0;i<data.length;i++){
			room.addLine({color:color,positions:data[i]});
		}

	},
	/**
	 * 初始化
	 */
	init:function(dom){
		var width = dom.width();
		var height = dom.height();
		init(dom);
		animate();
		function init( dom ) {
			room.setCamera(dom);
			room.setScene();
			var light = new THREE.PointLight( "#555555" );
			light.position.copy( camera.position );
			light.position.setZ(500);
			scene.add( light );
			
//===============================文字2====================================
			 room.addText({text:"机柜A1",color:"#ffffff",position:[-400,200,-350]});
			 room.addText({text:"机柜A2",color:"#ffffff",position:[-290,200,-350]});
			 room.addText({text:"机柜A3",color:"#ffffff",position:[-180,200,-350]});
			 room.addText({text:"机柜A4",color:"#ffffff",position:[-70,200,-350]});
			 
			 room.addText({text:"机柜B1",color:"#ffffff",position:[-400,200,50]});
			 room.addText({text:"机柜B2",color:"#ffffff",position:[-290,200,50]});
			 room.addText({text:"机柜B3",color:"#ffffff",position:[-180,200,50]});
			 room.addText({text:"机柜B4",color:"#ffffff",position:[-70,200,50]});
			 room.addText({text:"机柜B5",color:"#ffffff",position:[40,200,50]});
//===============================电源走线====================================
            var data = [
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -450],[10, 250, -450],[10, 50, -450],[10, 50, -400]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -450],[-100, 250, -450],[-100, 50, -450],[-100, 50, -400]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -450],[-210, 250, -450],[-210, 50, -450],[-210, 50, -400]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -450],[-315, 250, -450],[-315, 50, -450],[-315, 50, -400]],
							
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -250],[350, 250, -250],[350, 50, -250],[350, 50, -200]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -250],[240, 250, -250],[240, 50, -250],[240, 50, -200]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -250],[120, 250, -250],[120, 50, -250],[120, 50, -200]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -250],[10, 250, -250],[10, 50, -250],[10, 50, -200]],
							
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -50],[120, 250, -50],[120, 50, -50],[120, 50, 0]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -50],[10, 250, -50],[10, 50, -50],[10, 50, 0]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -50],[-100, 250, -50],[-100, 50, -50],[-100, 50, 0]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -50],[-210, 250, -50],[-210, 50, -50],[-210, 50, 0]],
							[[ -750, 350, 450],[ -550, 250, 450],[ -450, 250, 450],[-450, 250, -50],[-315, 250, -50],[-315, 50, -50],[-315, 50, 0]],
							
                        ];
			 
            room.addPowerLines(data, "#ff0000");
            var data2 = [
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -460],[0, 240, -460],[0, 50, -460],[0, 50, -410]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -460],[-110, 240, -460],[-110, 50, -460],[-110, 50, -410]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -460],[-220, 240, -460],[-220, 50, -460],[-220, 50, -410]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -460],[-335, 240, -460],[-335, 50, -460],[-335, 50, -410]],
                        
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -260],[340, 240, -260],[340, 50, -260],[340, 50, -210]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -260],[230, 240, -260],[230, 50, -260],[230, 50, -210]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -260],[110, 240, -260],[110, 50, -260],[110, 50, -210]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -260],[0, 240, -260],[0, 50, -260],[0, 50, -210]],
                        
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -60],[110, 240, -60],[110, 50, -60],[110, 50, 0]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -60],[0, 240, -60],[0, 50, -60],[0, 50, 0]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -60],[-110, 240, -60],[-110, 50, -60],[-110, 50, 0]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -60],[-220, 240, -60],[-220, 50, -60],[-220, 50, 0]],
                        [[ -750, 340, 450],[ -550, 240, 450],[ -450, 240, 450],[-450, 240, -60],[-325, 240, -60],[-325, 50, -60],[-325, 50, 0]],
                        
                        ];
            
            room.addPowerLines(data2, "#00ff00");
			 
//================================机柜===================================
            var imgs = [];
            for (var i = 1; i < 7; ++i) {
//            	imgs.push('img/' + i + '.jpg');
            }
            imgs.push('img/d.png');
            imgs.push('img/a.png');
            imgs.push('img/a.png');
            imgs.push('img/a.png');
            imgs.push('img/c.jpg');
            imgs.push('img/b.jpg');
            
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+100, 100, -450+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+210, 100, -450+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+320, 100, -450+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+430, 100, -450+100]});
            
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+430, 100, -250+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+540, 100, -250+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+650, 100, -250+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+760, 100, -250+100]});
            
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+100, 100, -50+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+210, 100, -50+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+320, 100, -50+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+430, 100, -50+100]});
            room.addCube({imgs:imgs,lwg:[100,200,100],position:[-450+540, 100, -50+100]});
            
//============================墙体=======================================
            room.addWall({color: "#AB8BAB",points:[[-500,0,500],[-500,0,-500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,0,-500],[500,0,-500]]});
            room.addWall({color: "#AB8BAB",points:[[500,0,-500],[500,0,500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,0,500],[-250,0,500]]});
            room.addWall({color: "#AB8BAB",points:[[-100,0,500],[500,0,500]]});
            
            room.addWall({color: "#111111",opa:true,points:[[-500,100,300],[-500,100,-300]]});
            room.addWall({color: "#AB8BAB",points:[[-500,100,500],[-500,100,300]]});
            room.addWall({color: "#AB8BAB",points:[[-500,100,-300],[-500,100,-500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,100,-500],[500,100,-500]]});
            room.addWall({color: "#111111",opa:true,points:[[500,100,-300],[500,100,300]]});
            room.addWall({color: "#AB8BAB",points:[[500,100,-500],[500,100,-300]]});
            room.addWall({color: "#AB8BAB",points:[[500,100,300],[500,100,500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,100,500],[-250,100,500]]});
            room.addWall({color: "#AB8BAB",points:[[-100,100,500],[500,100,500]]});
            
            room.addWall({color: "#AB8BAB",points:[[-500,200,500],[-500,200,-500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,200,-500],[500,200,-500]]});
            room.addWall({color: "#AB8BAB",points:[[500,200,-500],[500,200,500]]});
            room.addWall({color: "#AB8BAB",points:[[-500,200,500],[500,200,500]]});
            
            
//==========================地板=========================================
            room.addFloor({start:[-700,-700],end:[700,700],color: "#999999"});
            
            
            var loadingManager = new THREE.LoadingManager( function() {
				scene.add( elf );
			} );
			var loader = new THREE.ColladaLoader( loadingManager );
			loader.load( 'elf/avatar.dae', function ( collada ) {
//				loader.load( 'elf/elf.dae', function ( collada ) {
				elf = collada.scene;
			} );
            
	            
//=========================菱形==========================================
			var geometry = new THREE.CylinderGeometry( 0, 20, 50, 4, 1 );
			var material = new THREE.MeshPhongMaterial( { color: "#ff0000", flatShading: true } );
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.set(0,0,0);
			mesh.updateMatrix();
			mesh.matrixAutoUpdate = false;
			scene.add( mesh );
			
			
//===================================================================
			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( width,height);
			dom[0].appendChild( renderer.domElement );
			renderer.gammaInput = true;
			renderer.gammaOutput = true;
			controls = new THREE.OrbitControls( camera );
			window.addEventListener( 'resize', onWindowResize, false );
		}
		function onWindowResize() {
			camera.aspect = width/height;
			camera.updateProjectionMatrix();
			renderer.setSize( width,height );
		}
		function animate() {
			requestAnimationFrame( animate );
			if(auto){
				scene.rotation.set(0,scene.rotation._y+0.01,0);
			}
			renderer.render( scene, camera );
		}
	}
};